﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using Tanis.DataModels;
using Tanis.RouteService;
using Tanis.ViewModels;
using System.Windows.Media;
using System.Windows;

namespace Tanis.Controls
{
    public partial class RouteList
    {
        public RouteList()
        {
            InitializeComponent();
            App.ServiceManager.SearchPerformed += ServiceManagerOnSearchPerformed;
            App.ViewModel.SearchPerfoming += ViewModelSearchPerforming;
        }

        public Brush ThemeDependantColor { get { return new SolidColorBrush(ThemeHelper.ThemeDependantColor); } }
        public Brush ThemeDependantColorInverted { get { return new SolidColorBrush(ThemeHelper.ThemeDependantColorInverted); } }
        public Brush ThemeDependantListBoxColor { get { return new SolidColorBrush(ThemeHelper.ThemeDependantListBoxColor); } }

        private void ServiceManagerOnSearchPerformed(object sender, FindRoutesCompletedEventArgs args)
        {
            List<RouteSummary> summaryInfo = new List<RouteSummary>();

            if (args.Result != null && args.Result.Routes != null)
            {
                summaryInfo.AddRange(args.Result.Routes.Select(route => new RouteSummary(route)));
            }
            else
            {
                _infoMessage.Visibility = System.Windows.Visibility.Visible;
            }

            _routeListCtrl.ItemsSource = summaryInfo;

            ppbRouteList.Visibility = System.Windows.Visibility.Collapsed;
        }



        private void ViewModelSearchPerforming(object sender, System.EventArgs e)
        {
            _routeListCtrl.ItemsSource = null;
            ppbRouteList.Visibility = System.Windows.Visibility.Visible;
        }

        private void OnButtonClick(object sender, System.Windows.RoutedEventArgs e)
        {
            MainPage.MainPageInstance.RouteFinder.RouteDetailsRequested = true;
            MainPage.MainPageInstance.UpdatePanelVisibility();

            var routeSummary = ((Button)e.OriginalSource).DataContext as RouteSummary;
            App.ViewModel.OnDetailsRequested(new DetailsRequestedArgs(routeSummary));
        }
    }
}
